package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.DirectorateDao;
import com.mock.model.DataTable;
import com.mock.model.Directorate;
import com.mock.utils.HibernateUtil;

public class DirectorateDaoImp extends GenericDaoImpl<Directorate, Long> implements DirectorateDao{

	@Override
	public List<Directorate> getAllDirectorate() {
		return super.getAll(Directorate.class);
	}

	@Override
	public Directorate getById(Long id) {
		return super.getById(Directorate.class, id);
	}

	@Override
	public List<Directorate> getDirectorateByQuery(DataTable dataTable, long organisationId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Directorate.class, "directorate");
		criteria.createAlias("directorate.contact", "contact");
		criteria.setFetchMode("directorate.contact", FetchMode.JOIN);
		criteria.createAlias("directorate.organisation", "organisation");
		criteria.setFetchMode("directorate.organisation", FetchMode.JOIN);
		criteria.createAlias("directorate.country", "country");
		criteria.setFetchMode("directorate.country", FetchMode.JOIN);

		// get directorate follow organisationId
		criteria.add(Restrictions.eq("organisation.organisationId", organisationId));
		// Filter by Letter
				Disjunction disjunction = Restrictions.disjunction();
				if ("^[0-9]".equalsIgnoreCase(dataTable.sSearch_0)) {
					for (int i = 0; i < 10; i++) {
						disjunction
								.add(Restrictions.ilike("directorateName", i + "%"));
					}
					criteria.add(disjunction);
				} else {
					criteria.add(Restrictions.ilike("directorateName",
							dataTable.sSearch_0.replace("^", "") + "%"));
				}
		
		
		// Get sort column name
		String columnName = "directorateName";
		switch (dataTable.iSortCol_0) {
		case 0:
			columnName = "directorateName";
			break;
		case 1:
			columnName = "addressName1";
			break;
		case 2:
			columnName = "postcode";
			break;
		case 3:
			if ("asc".equalsIgnoreCase(dataTable.sSortDir_0)) {
				criteria.addOrder(Order.asc("contact.firstname"));
				criteria.addOrder(Order.asc("contact.surname"));
			} else {
				criteria.addOrder(Order.desc("contact.firstname"));
				criteria.addOrder(Order.desc("contact.surname"));
			}
			columnName = "contact.firstname";
			break;
		case 4:
			columnName = "active";
			break;
		default:
			break;
		}
		
		// Get Sort Direction
		if ("asc".equalsIgnoreCase(dataTable.sSortDir_0) && dataTable.iSortCol_0 != 3) {
				criteria.addOrder(Order.asc(columnName));
		} else {
				criteria.addOrder(Order.desc(columnName));
		}
		// Filter in-active
		if ("".equalsIgnoreCase(dataTable.sSearch_4)) {

		} else if ("Yes".equalsIgnoreCase(dataTable.sSearch_4)) {
				criteria.add(Restrictions.eq("active", true));
		}
		// Paging
		criteria.setFirstResult(dataTable.iDisplayStart);
		criteria.setMaxResults(dataTable.iDisplayLength);
		
		List<Directorate> listDirectorates = criteria.list();
		session.getTransaction().commit();
		return listDirectorates;
	}

	@Override
	public int getCountRow() {
		return super.getRowCount(Directorate.class);
	}

	@Override
	public List<Directorate> getDirectectorateByOrganisationId(
			long organisationId) {
		// TODO Auto-generated method stub
		return super.getListByColumn(Directorate.class, "organisationId", Long.toString(organisationId));
	}

}
